Skip to content

Earth - Kal#31

Open
kashenafi wants to merge 1 commit intoAda-C14:masterfrom
kashenafi:master
Open

Earth - Kal#31
kashenafi wants to merge 1 commit intoAda-C14:masterfrom
kashenafi:master

Conversation

@kashenafi
Copy link

@kashenafi kashenafi commented Jun 27, 2021

Heaps Practice

Congratulations! You're submitting your assignment!

Comprehension Questions

Question Answer
How is a Heap different from a Binary Search Tree? Binary Search Trees follow a specific order (preorder, inorder, etc.) and with heaps the parent is either less or bigger than children.
Could you build a heap with linked nodes? Yes - can't see why not.
Why is adding a node to a heap an O(log n) operation? It's O(log n) because you need to swap elements to restore heap property.
Were the heap_up & heap_down methods useful? Why? Yes, because it took care of all the swapping to restore the heap property.

@CheezItMan
Copy link

The disadvantage of building a Heap with a Linked List is that you can't jump in O(1) to a specific index, so finding the parent or child of a node would be O(n).

Copy link

@CheezItMan CheezItMan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well done Kal, you hit all the learning goals here. Great work!

Comment on lines +4 to +6
# Time Complexity: O(n log n)
# Space Complexity: O(n)
def heapsort(list)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Comment on lines +17 to 19
# Time Complexity: O(log n)
# Space Complexity: O(log n) - potential stack call for heap_up
def add(key, value = key)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Good on space/time complexity

Comment on lines +27 to 29
# Time Complexity: O(log n)
# Space Complexity: O(log n) - potential head_down stack call
def remove()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Good on space/time complexity

Comment on lines +57 to 59
# Time complexity: O(1)
# Space complexity: O(1)
def empty?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Comment on lines +68 to 70
# Time complexity: O(log n)
# Space complexity: O(log n)
def heap_up(index)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Good on space/time complexity

Comment on lines +88 to 90
# Time complexity: O(log n)
# Space complexity: O(log n)
def heap_down(index)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Nice work handling all 3 scenarios

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants